package com.itheima.reggie.Impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.CartService;
import com.itheima.reggie.OrderService;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private CartService cartService;
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public void post(Order order) {
        //获取那个用户
        User user = UserHolder.get();
        //获取用户的购物车
        List<Cart> list = cartService.find();
        //遍历之前判断
        if (CollectionUtil.isEmpty(list)){
            throw new CustomException("购物车为空");
        }
        //根据传过来的id拿到地址
        Address address = addressMapper.selectById(order.getAddressId());
        if (address==null){
            throw new CustomException("地址为空，不存在，滚你妈");
        }
        //雪花算法两种方法
        //long l = IdUtil.getSnowflake(1, 1).nextId();
        long id = IdWorker.getId();

        BigDecimal bigDecimal = new BigDecimal(0);
        //遍历之前添加金额

        //订单详情
        for (Cart cart : list)  {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(id);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setImage(cart.getImage());
            //保存到订单详细表
            orderDetailMapper.insert(orderDetail);

            bigDecimal = bigDecimal.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
        }
        //订单
        order.setId(id);//雪花算法
        order.setNumber(id+"");
        order.setStatus(0);
        order.setUserId(user.getId());//用户Id
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(bigDecimal);//总
        order.setUserName(user.getName());//用户的名字
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        //保存到订单表
        orderMapper.insert(order);

        //清空购物车
        cartService.deleted();
    }

    @Override
    public Page<Order> findById(Integer page, Integer pageSize) {
        //分页条件
        Page<Order> orderPage = new Page<>(page, pageSize);
        //设置条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getUserId, UserHolder.get().getId());
        //执行查询
        orderPage=orderMapper.selectPage(orderPage, wrapper);
        return orderPage;
    }
    }
